<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Transformers\NotificationTransformer;
use App\Models\User;
class NotificationsController extends Controller
{
    public function index(Request $request)
    {
        // 获取登录用户的所有通知
        if ($id = $request->input('id')) {
            $user = User::find($id);
        } else {
            $user = $this->user();
        }

        $notifications = $user->notifications()->paginate(10);
        return $this->response->paginator($notifications, new NotificationTransformer);
    }
    public function stats()
    {
        $count = $this->user()->notification_count;
        return $this->response->array(['unread_count' => $count]);
    }

    public function read()
    {
        $this->user()->markAsRead();

        return $this->response->noContent();
    }
}
